// components/stepper/stepper.js

var utils_1 = require('../../libs/@vant/weapp/common/utils');

// add num and avoid float number
function add(num1, num2) {
  var cardinal = Math.pow(10, 10);
  return Math.round((num1 + num2) * cardinal) / cardinal;
}
function equal(value1, value2) {
  return String(value1) === String(value2);
}

var LONG_PRESS_START_TIME = 600;
var LONG_PRESS_INTERVAL = 200;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: null,
      observer(value) {
        if (!equal(value, this.data.currentValue)) {
          this.setData({ currentValue: this.format(value) });
        }
      }
    },
    integer: {
      type: Boolean,
      observer: 'check',
    },
    decimalLength: {
      type: Number,
      value: null,
      observer: 'check',
    },
    min: {
      type: null,
      value: 0,
      observer: 'check',
    },
    max: {
      type: null,
      value: Number.MAX_SAFE_INTEGER,
      observer: 'check',
    },
    step: {
      type: null,
      value: 1,
    },
    longPress: {
      type: Boolean,
      value: true,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentValue: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onTap(event) {
      var type = event.currentTarget.dataset.type;
      this.type = type;
      this.onChange();
    },
    onTouchStart: function (event) {
      var _this = this;
      if (!this.data.longPress) {
        return;
      }
      clearTimeout(this.longPressTimer);
      var type = event.currentTarget.dataset.type;
      this.type = type;
      this.isLongPress = false;
      this.longPressTimer = setTimeout(function () {
        _this.isLongPress = true;
        _this.onChange();
        _this.longPressStep();
      }, LONG_PRESS_START_TIME);
    },
    onTouchEnd: function () {
      if (!this.data.longPress) {
        return;
      }
      clearTimeout(this.longPressTimer);
    },
    longPressStep: function () {
      var _this = this;
      this.longPressTimer = setTimeout(function () {
        _this.onChange();
        _this.longPressStep();
      }, LONG_PRESS_INTERVAL);
    },
    emitChange: function (value) {
      this.setData({ currentValue: value });
      this.triggerEvent('change', value);
    },
    onChange: function () {
      var type = this.type;
      // if (this.isDisabled(type)) {
      //   this.$emit('overlimit', type);
      //   return;
      // }
      var diff = type === 'minus' ? -this.data.step : +this.data.step;
      var value = this.format(add(+this.data.currentValue, diff));
      this.emitChange(value);
      this.triggerEvent(type);
    },
    check: function () {
      var val = this.format(this.data.currentValue);
      if (!equal(val, this.data.currentValue)) {
        this.setData({ currentValue: val });
      }
    },
    // filter illegal characters
    filter: function (value) {
      value = String(value).replace(/[^0-9.-]/g, '');
      if (this.data.integer && value.indexOf('.') !== -1) {
        value = value.split('.')[0];
      }
      return value;
    },
    format: function (value) {
      value = this.filter(value);
      // format range
      value = value === '' ? 0 : +value;
      value = Math.max(Math.min(this.data.max, value), this.data.min);
      // format decimal
      if (utils_1.isDef(this.data.decimalLength)) {
        value = value.toFixed(this.data.decimalLength);
      }
      return value;
    },
  },
  attached: function () {
    this.setData({
      currentValue: this.format(this.data.value),
    });
  },
})
